package main

import (
	"fmt"
	"math"
	"strings"
	"time"
)

func myAtoi(s string) int {
	flag := false
	j := 0
	result := 0

	//去除空格
	s = strings.TrimSpace(s)
	if len(s) == 0 {
		return 0
	}

	if s[0] == '-' {
		flag = true
		j = 1
	}
	if s[0] == '+' {
		j = 1
	}

	for i := j; i < len(s); i++ {

		if '0' <= s[i] && s[i] <= '9' {
			result = result*10 + int(s[i]-'0')
		} else {
			break
		}
		if result > math.MaxInt32 {
			break
		}
	}
	if flag == true {
		result = -result
	}
	if result < math.MinInt32 {
		result = math.MinInt32
	}
	if result > math.MaxInt32 {
		result = math.MaxInt32
	}

	return result
}

func main() {
	start := time.Now()
	s := "9223372036854775808"
	result := myAtoi(s)
	cost := time.Since(start)
	fmt.Println("result:", result)
	fmt.Println(cost)

}
